package com.eheart.demo_jdk21.feature;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class VirtualThread {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            executorService.submit(() -> {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return 0;
            });
        }
        executorService.close();
        long lastTime = System.currentTimeMillis();
        long result = lastTime - startTime;
        System.out.println("result = " + result);

        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        long startDate = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            threadPool.submit(()->{
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return 0;
            });
        }

        threadPool.close();
        long endDate = System.currentTimeMillis();
        long result2 = endDate - startDate;
        System.out.println("result2 = " + result2);
    }

}
